package com.woniu.microworkorderserver.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.woniu.microcommonsserver.entity.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JsonToObject {

    public static List<PollingTemplateInfoEntity> getPollingTemplates(JSONObject json){
        JSONArray str = json.getJSONArray("inspectTemplate");
        List<PollingTemplateInfoEntity> list = new ArrayList<>();
        PollingTemplateInfoEntity entity = new PollingTemplateInfoEntity();
        for (int i = 0; i < str.size(); i++){
            JSONObject jsObject = str.getJSONObject(i);
            long id = 0;
            if (jsObject.getString("id") != ""){
                id = Long.parseLong(jsObject.getString("id"));
            }
            entity.setId(id);
            entity.setTemplateName(jsObject.getString("templateName"));
            entity.setTemplateType(jsObject.getString("templateType"));
            entity.setAssetType(jsObject.getString("assetType"));
            entity.setSpaceType(jsObject.getString("spaceType"));
            entity.setContent(jsObject.getString("content"));
            list.add(entity);
        }
        return list;
    }

    public static List<InspectItemInfoEntity> getInspectItems(JSONObject json){
        JSONArray str = json.getJSONArray("inspectItem");
        List<InspectItemInfoEntity> list = new ArrayList<>();
        InspectItemInfoEntity entity = new InspectItemInfoEntity();
        for (int i = 0; i < str.size(); i++){
            JSONObject jsObject = str.getJSONObject(i);
            long id = 0;
            if (jsObject.getString("id") != "" && jsObject.getString("id") !=null){
                id = Long.parseLong(jsObject.getString("id"));
            }
            entity.setId(id);
            entity.setInspectName(jsObject.getString("inspectName"));
            entity.setType(jsObject.getString("type"));
            entity.setContent(jsObject.getString("content"));
            list.add(entity);
        }
        return list;
    }

    public static List<WorkGroupInfoEntity> getWorkGroups(JSONObject json){
        JSONArray str = json.getJSONArray("workGroup");
        List<WorkGroupInfoEntity> list = new ArrayList<>();
        WorkGroupInfoEntity entity = new WorkGroupInfoEntity();
        for (int i = 0; i < str.size(); i++){
            JSONObject jsObject = str.getJSONObject(i);
            long id = 0;
            if (jsObject.getString("id") != "" && jsObject.getString("id") != null){
                id = Long.parseLong(jsObject.getString("id"));
            }
            entity.setId(id);
            entity.setWorkGroupName(jsObject.getString("name"));
            entity.setContent(jsObject.getString("content"));
            entity.setProject(jsObject.getString("project"));
            list.add(entity);
        }
        return list;
    }

    public static List<Integer> getWorkUserIds(JSONObject json){
        JSONArray str = json.getJSONArray("workUser");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < str.size(); i++){
            list.add(str.getInteger(i));
        }
        return list;
    }

    public static List<PollingPlanInfoEntity> getPollingPlans(JSONObject json){
        JSONArray str = json.getJSONArray("inspectPlanData");
        List<PollingPlanInfoEntity> list = new ArrayList<>();
        PollingPlanInfoEntity entity = new PollingPlanInfoEntity();
        for (int i = 0; i < str.size(); i++){
            JSONObject jsObject = str.getJSONObject(i);
            long id = 0;
            if (jsObject.getString("id") != "" && jsObject.getString("id") != null){
                id = Long.parseLong(jsObject.getString("id"));
            }
            entity.setId(id);
            entity.setProject(jsObject.getString("project"));
            entity.setPlanName(jsObject.getString("planName"));
            entity.setExpectTime(Integer.parseInt(jsObject.getString("expectTime")));
            entity.setContent(jsObject.getString("content"));
            Date data = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
            try{
                data = dateFormat.parse(jsObject.getString("inspectTime"));
            }catch (Exception e){
                e.printStackTrace();
            }
            entity.setInspectTime(data);
            list.add(entity);
        }
        return list;
    }

    public static List<InspectPointInfoEntity> getInspectPoints(JSONObject json){
        JSONArray str = json.getJSONArray("inspectPointData");
        List<InspectPointInfoEntity> list = new ArrayList<>();
        InspectPointInfoEntity entity = new InspectPointInfoEntity();
        for (int i = 0; i < str.size(); i++){
            JSONObject jsObject = str.getJSONObject(i);
            long id = 0;
            if (jsObject.getString("id") != "" && jsObject.getString("id") != null){
                id = Long.parseLong(jsObject.getString("id"));
            }
            entity.setId(id);
            entity.setInspectPoint(jsObject.getString("project"));
            entity.setFkInspectTemId(getInspectTemIds(json).get(0));
            entity.setType(jsObject.getString("assetType"));
            entity.setLocation(jsObject.getString("space"));
            list.add(entity);
        }
        return list;
    }

    public static List<Long> getInspectTemIds(JSONObject json){
        JSONArray str = json.getJSONArray("inspectTemplateId");
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < str.size(); i++){
            list.add(str.getLong(i));
        }
        return list;
    }


}
